﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Generic;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using WX.WMS.BLL;
using WX.WMS.Common;
using WX.WMS.DataModel;
using System.Linq;
using System.Data.Linq;
using System.Text;
using System.Threading;
using System.Globalization;

/// <summary>
/// PageBase Summary
/// </summary>
public class PageBase : System.Web.UI.Page
{
    //LogManager logManager = LogManager.GetInstance();

    protected string msg
    {
        get { return  Convert.ToString(ViewState["msg"]); }
        set { ViewState["msg"] = value; }
    }
    /// <summary>
    /// 
    /// </summary>
    public PageBase()
    {

    }
    protected override void Render(HtmlTextWriter writer)
    {
        
        base.Render(writer);
        if (!string.IsNullOrEmpty(msg))
        {
            writer.Write("<script>" + msg + "</script>");
            msg = "";
        }
    }

    //protected void Page_Error(object sender, EventArgs e)
    //{
    //    Exception objErr = Server.GetLastError().GetBaseException();

    //    //write the log

    //    logManager.WriteErrorLog(objErr.Message);

    //    //
    //    Server.ClearError(); //be noted the code.
    //    throw objErr;
    //}

 
     //<summary>
     //init the component
     //</summary>
     //<param name="e"></param>
    protected override void OnInit(EventArgs e)
    {


        if (this.UserInfo == null) Response.Redirect("~/login.aspx");
        base.OnInit(e);
    }

    //protected virtual void SetModuleLabel(int moduleId)
    //{
    //    if (this.Master == null) return;
    //    Label lblModuleName = (Label)this.Master.FindControl("lblModuleName");

    //    if (lblModuleName == null) return;      

    //    switch(this._moduleId)
    //    {
    //        case 0:
    //            lblModuleName.Text = "User Management";
    //            break;
    //        case 1:
    //            lblModuleName.Text = "Phone Management";
    //            break;
    //        case 2:
    //            lblModuleName.Text = "Battery Management";
    //            break;
    //        case 3:
    //            lblModuleName.Text = "Search Phone By Battery";
    //            break;
    //        case 4:
    //            lblModuleName.Text = "Search Battery By Phone";
    //            break;
     
    //     }

    //}


    //protected override void Render(HtmlTextWriter writer)
    //{
    //    base.Render(writer);     
    //}

    //protected virtual void SetMouduleId()
    //{

    //}

  

    ///// <summary>
    ///// Initialization the component, check if the session is null
    ///// </summary>
    //protected virtual void InitializeComponent()
    //{
    //    ////login page settings
    //    string returnUrl = ApplicationPath + "login.aspx";

    //    ////if the value of the userid session is null,the system will logout to the login ui.
    //   if (UserInfo==null)
    //   {
    //       Response.Write("<script language='javascript'>alert('You have not login the system or the link information is lost. Please login the system again!');window.top.location='" + returnUrl + "';</script>");
    //   }
    //}

    ///// <summary>
    ///// User information
    ///// </summary>
    public T_SYS_USER  UserInfo
    {
        get
        {
            if (Session["UserInfo"] != null)
            {
                return (T_SYS_USER)Session["UserInfo"];
            }
            else
            {
                return null;
            }
        }
        set
        {
            Session["UserInfo"] = value;
        }
    }   


    //Int32 _moduleId = -1;
    //public Int32 CurrentModuleId
    //{
    //    get
    //    {
    //        return _moduleId;
    //    }
    //    set
    //    {
    //        _moduleId = value;
    //    }
    //}

    /// <summary>
    /// virtual path
    /// </summary>
    //public static string ApplicationPath
    //{

    //    get
    //    {
    //        if (ConfigurationManager.AppSettings["DebugMode"] == "0")
    //            return ConfigurationManager.AppSettings["VirturlPath"];
    //        else
    //        {

    //            if (System.Web.HttpContext.Current == null)
    //                return "";

    //            if (System.Web.HttpContext.Current.Cache["ApplicationPath"] == null)
    //            {
    //                System.Web.HttpContext.Current.Cache["ApplicationPath"] = System.Web.HttpContext.Current.Request.ApplicationPath;
    //                if (System.Web.HttpContext.Current.Cache["ApplicationPath"].ToString() == "/")
    //                {
    //                    System.Web.HttpContext.Current.Cache["ApplicationPath"] = "";
    //                }
    //            }
    //            return System.Web.HttpContext.Current.Cache["ApplicationPath"].ToString();
    //        }
    //    }
    //}


    /// <summary>
    /// check if the user has the module privilege 
    /// </summary>
    /// <returns></returns>
    //public bool HasPrivilege()
    //{
    //    if (UserInfo == null)
    //    {
    //        string url;
    //        url = ApplicationPath + "Login.aspx";
    //        Response.Redirect(url);

    //       return false;           
    //    }
    //    if (UserInfo.Type == (int)Enumerations.UserType.Administrator)
    //    {
    //        return true;            
    //    }
    //    else
    //    {
    //        switch (CurrentModuleId)
    //        {

    //            case (int)SystemModules.UserManagement:
    //                return false;


    //            case (int)SystemModules.PhoneManagement:
    //                return false;


    //            case (int)SystemModules.BatteryManagement:
    //                return false;

    //            default:
    //                return true;
    //        }           
    //    }
       
    //}

    ///// <summary>
    ///// check if the user has the module privilege
    ///// </summary>
    ///// <param name="moduleId"> the module id</param>
    ///// <returns>bool</returns>
    //public bool HasPrivilege(int moduleId)
    //{
    //    return true;
    //}

    protected void ShowMessage(string msg)
    {
        ShowMessage(msg, null);
    }

    protected void ShowMessage(string msg, string detail)
    {
        if (string.IsNullOrEmpty(detail))
        {
            //Response.Write("<script > window.alert('" + msg + "'); </script>");
            msg = "alert('" + msg + "'); ";
            Page.ClientScript.RegisterStartupScript(typeof(Page), "afq", msg ,true);

        }
        else
        {//&#13;&#10;
            detail = detail.Replace("\n", "\\n");
            detail = detail.Replace("\r", "\\r");
            detail = detail.Replace("'", "\\'");
           // Response.Write("<script > window.alert(\"" + msg + "\\n" + detail + "\"); </script>");
            msg=" alert(\"" + msg + "\\n" + detail + "\"); ";
            Page.ClientScript.RegisterStartupScript(typeof(Page), "afq", msg, true);
        }
    }

    protected void AjaxShowMessage(UpdatePanel panel,string msg)
    {
        msg = msg.Replace("'", "\\'");
        ScriptManager.RegisterClientScriptBlock(panel, panel.GetType(), "BatteryMatrix", "window.alert('" + msg + "');", true);
    }
}
